package cn.iocoder.yudao.module.peizhen.controller.admin.notice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.peizhen.controller.admin.notice.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.notice.NoticePeiZhenDO;
import cn.iocoder.yudao.module.peizhen.service.notice.NoticePeiZhenService;

@Tag(name = "管理后台 - 通知")
@RestController
@RequestMapping("/peizhen/notice-pei-zhen")
@Validated
public class NoticePeiZhenController {

    @Resource
    private NoticePeiZhenService noticePeiZhenService;

    @PostMapping("/create")
    @Operation(summary = "创建通知")
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:create')")
    public CommonResult<Integer> createNoticePeiZhen(@Valid @RequestBody NoticePeiZhenSaveReqVO createReqVO) {
        return success(noticePeiZhenService.createNoticePeiZhen(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通知")
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:update')")
    public CommonResult<Boolean> updateNoticePeiZhen(@Valid @RequestBody NoticePeiZhenSaveReqVO updateReqVO) {
        noticePeiZhenService.updateNoticePeiZhen(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通知")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:delete')")
    public CommonResult<Boolean> deleteNoticePeiZhen(@RequestParam("id") Integer id) {
        noticePeiZhenService.deleteNoticePeiZhen(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:query')")
    public CommonResult<NoticePeiZhenRespVO> getNoticePeiZhen(@RequestParam("id") Integer id) {
        NoticePeiZhenDO noticePeiZhen = noticePeiZhenService.getNoticePeiZhen(id);
        return success(BeanUtils.toBean(noticePeiZhen, NoticePeiZhenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通知分页")
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:query')")
    public CommonResult<PageResult<NoticePeiZhenRespVO>> getNoticePeiZhenPage(@Valid NoticePeiZhenPageReqVO pageReqVO) {
        PageResult<NoticePeiZhenDO> pageResult = noticePeiZhenService.getNoticePeiZhenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NoticePeiZhenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通知 Excel")
    @PreAuthorize("@ss.hasPermission('peizhen:notice-pei-zhen:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNoticePeiZhenExcel(@Valid NoticePeiZhenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NoticePeiZhenDO> list = noticePeiZhenService.getNoticePeiZhenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通知.xls", "数据", NoticePeiZhenRespVO.class,
                        BeanUtils.toBean(list, NoticePeiZhenRespVO.class));
    }

}